<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class PenerimaanObat extends zulController {
	
	
	function __construct(){
		parent::__construct();
		$this->load->model("PenerimaanObatModel");
		$this->checkLogin();
	}
	public function index()
	{
		$colModel['NO'] = array('No', 20, TRUE, 'center', 0);
        $colModel['NO_FAKTUR'] = array('Nomor Faktur', 110, FALSE, 'center', 1);
		$colModel['TGL_PENERIMAAN'] = array('Tanggal Penerimaan', 120, FALSE, 'center', 1);
		$colModel['LIHAT DETAIL'] = array('Lihat Detail', 110, FALSE, 'center', 0);
		$colModel['EDIT'] = array('Edit', 70, FALSE, 'center', 0);
        $gridParams = array(
            'width' => 'auto',
            'height' => '390',
            'rp' => 15,
            'rpOptions' => '[10,15,20,25,40]',
            'pagestat' => 'Displaying: {from} to {to} of {total} items.',
            'blockOpacity' => 0.5,
            'title' => 'Daftar Penerimaan Obat'
        );
        $buttons[] = array('Tambah', 'add', 'PenerimaanObat');
        // $buttons[] = array('Hapus', 'delete', 'PenerimaanObat');
        $buttons[] = array('separator');
        $buttons[] = array('Pilih Semua', 'add', 'PenerimaanObat');
        $buttons[] = array('Hapus Pilihan', 'delete', 'PenerimaanObat');
        $buttons[] = array('separator');
		
        $grid_js = build_grid_js('flex1', base_url("index.php/PenerimaanObat/gridPenerimaanObat"), $colModel, 'ID_PENERIMAAN_OBAT', 'ASC', $gridParams, $buttons);

        $data['title'] = "Daftar Penerimaan Obat";
        $data['info'] = "Daftar Penerimaan Obat";
        $data['js_grid'] = $grid_js;


        $data['added_js'] =
                "<script type='text/javascript'>
			function PenerimaanObat(com,grid)
			{
				if (com=='Pilih Semua')
				{
					$('.bDiv tbody tr',grid).addClass('trSelected');
				}
				
				if (com=='Hapus Pilihan')
				{
					$('.bDiv tbody tr',grid).removeClass('trSelected');
				}
				
				if (com=='Tambah')
				{
					location.href='" . site_url() . "/PenerimaanObat/formPenerimaanObat/'; 
				}
				
				if (com=='Hapus')
				{
				   if($('.trSelected',grid).length>0){
					   if(confirm('Anda yakin ingin mengaktifkan ' + $('.trSelected',grid).length + ' buah data?')){
							var items = $('.trSelected',grid);
							var itemlist ='';
							for(i=0;i<items.length;i++){
								itemlist+= items[i].id.substr(3)+',';
							}
							$.ajax({
							   type: 'POST',
							   url: '" .site_url('/PenerimaanObat/deletePenerimaanObat')."',
							   data: 'items='+itemlist,
							   success: function(data){
								$('#flex1').flexReload();
								alert(data);
							   }
							});
						}
					} else {
						return false;
					} 
				}
			} 
			</script>
			";
		$data["active"] = "transaksi";
		$data["content"]	= $this->load->view("grid",$data,true);
		$this->load->view("main_new",$data);
	}
	function gridPenerimaanObat(){
		$valid_fields = array('ID_PENERIMAAN_OBAT', 'NO_FAKTUR', 'TGL_PENERIMAAN');
        $this->flexigrid->validate_post('ID_PENERIMAAN_OBAT', 'ASC', $valid_fields);
        $records = $this->PenerimaanObatModel->gridPenerimaanObat();
        $this->output->set_header($this->config->item('json_header'));
        $no = 1;
        foreach ($records['records']->result() as $row) {
            $record_items[] = array($row->ID_PENERIMAAN_OBAT,
                $no,
				$row->NO_FAKTUR,
				$this->dateFormat($row->TGL_PENERIMAAN),
				"<a class='fancybox fancybox.ajax' href='".site_url("/PenerimaanObat/viewDetail/".$row->ID_PENERIMAAN_OBAT)."'><img src='". base_url()."assets/images/grid/magnifier.png'></a>",
				"<a href=\"".site_url("/PenerimaanObat/formPenerimaanObat/".$row->ID_PENERIMAAN_OBAT)."\"><img src=\"". base_url()."assets/images/grid/edit.png\"></a>"
            );
            $no++;
        }
        if (isset($record_items))
            $this->output->set_output($this->flexigrid->json_build($records['record_count'], $record_items));
        else
            $this->output->set_output('{"page":"1","total":"0","rows":[]}');
	}
	function formPenerimaanObat($parameter=FALSE){
		$this->load->library("form_validation");
		$this->load->model("ObatModel");
		$this->load->model("DetailPenerimaanModel");
		$this->load->model("HistoryHargaModel");
		$this->form_validation->set_rules('tgl_terima','Tanggal Penerimaan','required');
		if($parameter == FALSE){
			$this->form_validation->set_rules('no_faktur','Nomor Faktur','required|callback_checkFaktur');
		} else {
			$check	= $this->PenerimaanObatModel->get($parameter);
			if($check["NO_FAKTUR"] == $this->input->post("no_faktur")){
				$this->form_validation->set_rules('no_faktur','Nomor Faktur','required');
			} else {
				$this->form_validation->set_rules('no_faktur','Nomor Faktur','required|callback_checkFaktur');
			}
		}
		$this->form_validation->set_error_delimiters('<div class="alert alert-error fade in"><button type="button" class="close" data-dismiss="alert">&times;</button>', '</div>');
		$this->form_validation->set_message('required','Kolom %s harus diisi !');
		if($this->form_validation->run() == FALSE){
			if($parameter == TRUE){
				$data["value"] 		= $this->PenerimaanObatModel->get($parameter);
				$join				= array(
										array("table"=>"detail_penerimaan","join_key"=>"ID_HISTORY_HARGA","join_table"=>"history_harga"),
										array("table"=>"history_harga","join_key"=>"ID_OBAT","join_table"=>"obat")
									);
				$data["getDetail"]	= $this->DetailPenerimaanModel->get_join($join,$parameter);
			}
			$data["getObat"]	= $this->ObatModel->get(FALSE,FALSE);
			$data["parameter"]	= $parameter;
			$data["active"] = "transaksi";
			$data["content"]	= $this->load->view("formPenerimaanObat",$data,true);
			$this->load->view("main_new",$data);
		} else {
			$no_faktur		= $this->input->post("no_faktur");
			$tgl_terima	= $this->input->post("tgl_terima");
			$data	= array(
			"NO_FAKTUR"=>$no_faktur,
			"TGL_PENERIMAAN"=>$tgl_terima,
			);
			$id_penerimaan	= $this->PenerimaanObatModel->save($data, $parameter);
			$this->DetailPenerimaanModel->delete($id_penerimaan);
			$getObat		= $this->ObatModel->get(FALSE,FALSE);
			foreach($getObat as $row){
				$jumlah	= $this->input->post("jumlah".$row["ID_OBAT"]);
				if($jumlah != NULL){
					$id_history	= $this->HistoryHargaModel->get_by(array("ID_OBAT"=>$row["ID_OBAT"]),FALSE,FALSE,TRUE);
					$data	= array(
					"ID_PENERIMAAN_OBAT"=>$id_penerimaan,
					"ID_HISTORY_HARGA"=>$id_history["ID_HISTORY_HARGA"],
					"JUMLAH"=>$jumlah
					);
					$this->DetailPenerimaanModel->save($data);
				}
			}
			redirect("/PenerimaanObat");
		}
	}
	function checkFaktur($no){
		$check	= $this->PenerimaanObatModel->get_by(array("NO_FAKTUR"=>$no));
		if(count($check) > 0){
			$this->form_validation->set_message("checkFaktur","Nomor Faktur sudah dipakai.");
			return FALSE;
		} else {
			return TRUE;
		}
	}
	function viewDetail($parameter){
		$this->load->model("DetailPenerimaanModel");
		$join	= array(
			array("table"=>"detail_penerimaan","join_key"=>"ID_HISTORY_HARGA","join_table"=>"history_harga"),
			array("table"=>"history_harga","join_key"=>"ID_OBAT","join_table"=>"obat")
			);
		$data["getDetail"]	= $this->DetailPenerimaanModel->get_join($join,$parameter);
		$this->load->view("detailPenerimaan",$data);
	}
	function deletePenerimaanObat(){
		$ids	= $this->input->post('items');
		$this->PenerimaanObatModel->delete($ids);
		$msg ='Data yang anda pilih berhasil dihapus ';
		$this->output->set_output($msg);
	}
}